//
//  RequestApi+Auth.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/27.
//

#import "RequestApi+Auth.h"

@implementation RequestApi (Auth)

#pragma mark - 检查用户是否可以做某项授权
+ (void)requestCheckUserCanAuthWithType:(NSString *)authType
                               phone:(NSString *)phone
                                  delegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                                failure:(void (^)(NSString * errorStr, id mark))failure{
    NSString * url = [NSString stringWithFormat:@"%@%@%@",URL_HEAD, AUTH_CHECK_NEW, authType];
    if (isStr(phone)) {
        url = [NSString stringWithFormat:@"%@?%@=%@", url, KEY_phone, phone];
    }
    [RequestApi postUrl:url delegate:delegate parameters:nil success:success failure:failure];
}

#pragma mark - 获取活体渠道接口-新
+ (void)requestNewAuthLivenConfigWithPhone:(NSString *)phone
                                  delegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    NSString * url = [NSString stringWithFormat:@"%@%@",URL_HEAD, AUTH_lIVENCONFIG_NEW];
    if (isStr(phone)) {
        url = [NSString stringWithFormat:@"%@?%@=%@", url, KEY_phone, phone];
    }
    [RequestApi postUrl:url delegate:delegate parameters:nil success:success failure:failure];
}

#pragma mark - 获取下一步该填写信息的step
+ (void)requestGetNextStepNewWithDelegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@",URL_HEAD, LOAN_GET_NEXT_STEP_NEW] delegate:delegate parameters:nil success:success failure:failure];
}

#pragma mark - 获取所有步骤的奖励金钱
+ (void)requestAwardAmountWithDelegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@",URL_HEAD, AUTH_GETAWARDAMOUNT_NEW] delegate:delegate parameters:nil success:success failure:failure];
}

#pragma mark - 保存用户信息
+ (void)requestSaveUserInfoWithData:(NSDictionary *)data
                                      step:(NSString *)step
                                  delegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    // step: 属于哪一个页面
    NSDictionary * dic = @{
        KEY_data: data,
    };
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@/%@",URL_HEAD, AUTH_SAVE_USER_INFO_NEW,step] delegate:delegate parameters:dic success:success failure:failure];
}

#pragma mark - 保存用户信息 - 银行卡认证
+ (void)requestSaveUserInfoAuthBankCardWithData:(NSDictionary *)data
                                      step:(NSString *)step
                                  delegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    // step: 属于哪一个页面
    NSDictionary * dic = @{
        KEY_data: data,
    };
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@/%@",URL_HEAD, AUTH_SAVE_USER_INFO_AUTO_APPLY,step] delegate:delegate parameters:dic success:success failure:failure];
}

#pragma mark - 上传身份证正面并OCR
+ (void)requestOSSUploadPositiveWithImage:(UIImage *)image
                          ocrPhotoChannel:(NSInteger)ocrPhotoChannel
                                  delegate:(id <RequestDelegate> _Nullable)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    NSString * url = [NSString stringWithFormat:@"%@%@?%@=%@",URL_HEAD, COMMON_OSS_UPLOAD_POSITIVE, KEY_ocrPhotoChannel, [NSString stringWithFormat:@"%ld",ocrPhotoChannel]];
    // 压缩图片
    NSData * imageData = [GlobalMethod compressImage:image toMaxSize:300];
    NSUInteger maxSize = 300 * 1024;
    for (int i = 0; i < 3; i++) {
        if (imageData.length > maxSize) {
            imageData = [GlobalMethod compressImage: [UIImage imageWithData:imageData] toMaxSize:300];
        } else {
            break;
        }
    }
    // 最后再检查一次
    if (imageData.length > maxSize) {
        imageData = [GlobalMethod compressImage:[UIImage imageWithData:imageData] toMaxSize:300];
    }
    NSLog(@"Initial image size ------ : %lu KB", (unsigned long)(imageData.length / 1024));
    
    // 打点
    [GlobalMethod hitPointWithPageName:KEY_VerifyKtp action:KEY_ID_Front_Camera_Save extInfo:@{@"size":[NSString stringWithFormat:@"%lukb",(unsigned long)(imageData.length / 1024)]}];
    [GlobalMethod hitPointWithPageName:KEY_VerifyKtp action:KEY_ID_Front_Request extInfo:nil];
    [RequestApi postUrl:url delegate:delegate parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        // 将图片转换为JPEG格式的NSData对象
        // 添加数据到请求体
        [formData appendPartWithFileData:imageData
                                    name:@"photo"
                                fileName:@"image.jpg"
                                mimeType:@"image/jpeg"];
    } success:success failure:failure];
}

#pragma mark - SDK活体预处理接口
+ (void)requestPreLivenessWithLivenessId:(NSString *)livenessId
                                  delegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    NSString * url = [NSString stringWithFormat:@"%@%@?%@=%@",URL_HEAD, AUTH_PRE_lIVENESS, KEY_livenessId, livenessId];
    [RequestApi postUrl:url delegate:delegate parameters:nil success:success failure:failure];
}

#pragma mark - 活体照片识别
+ (void)requestAuthLiveWithImage:(UIImage *)image
                                phone:(NSString *)phone
                     livenChannelEnum:(NSString *)livenChannelEnum
                         liveTypeEnum:(NSString *)liveTypeEnum
                                  delegate:(id <RequestDelegate> _Nullable)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    NSString * url = [NSString stringWithFormat:@"%@%@%@/%@",URL_HEAD,COMMON_AUTH_LIVE,livenChannelEnum,liveTypeEnum];
    if (isStr(phone)) {
        url = [NSString stringWithFormat:@"%@?%@=%@", url, KEY_phone, phone];
    }
    // 压缩图片
    NSData * imageData = [GlobalMethod compressImage:image toMaxSize:300];
    NSUInteger maxSize = 300 * 1024;
    for (int i = 0; i < 3; i++) {
        if (imageData.length > maxSize) {
            imageData = [GlobalMethod compressImage: [UIImage imageWithData:imageData] toMaxSize:300];
        } else {
            break;
        }
    }
    // 最后再检查一次
    if (imageData.length > maxSize) {
        imageData = [GlobalMethod compressImage:[UIImage imageWithData:imageData] toMaxSize:300];
    }
    NSLog(@"Initial image size ------ : %lu KB", (unsigned long)(imageData.length / 1024));
    [RequestApi postUrl:url delegate:delegate parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        // 将图片转换为JPEG格式的NSData对象
        // 添加数据到请求体
        [formData appendPartWithFileData:imageData
                                    name:@"photo" // photo - trullemos
                                fileName:@"image.jpg"
                                mimeType:@"image/jpeg"];
    } success:success failure:failure];
}

#pragma mark - 切换活体渠道接口
+ (void)requestChangeLiveChanelWithPhone:(NSString *)phone
                                delegate:(id <RequestDelegate>)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    NSString * url = [NSString stringWithFormat:@"%@%@",URL_HEAD, AUTH_CHANGE_LIVE_CHANEL];
    if (isStr(phone)) {
        url = [NSString stringWithFormat:@"%@?%@=%@", url, KEY_phone, phone];
    }
    [RequestApi postUrl:url delegate:delegate parameters:nil success:success failure:failure];
}

#pragma mark - 获取cloudun sdk授权信息
+ (void)requestCloudunSdkAuthInfoWithDelegate:(id <RequestDelegate> _Nullable)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    NSString * url = [NSString stringWithFormat:@"%@%@",URL_HEAD, COMMON_GET_CLOUDUN_PUREH5_INFO];
    [RequestApi postUrl:url delegate:delegate parameters:nil success:success failure:failure];
}

#pragma mark - 是否显示自动首借弹窗
+ (void)requestShowAutoFirstLoanPopWindowWithDelegate:(id <RequestDelegate> _Nullable)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                                              failure:(void (^)(NSString * errorStr, id mark))failure{
    [RequestApi postUrl:[NSString stringWithFormat:@"%@%@",URL_HEAD, AUTH_SHOW_AUTO_FIRST_LOAN] delegate:delegate parameters:nil success:success failure:failure];
}
#pragma mark - 检查修改银行卡活体
+ (void)requestCheckModifyBankCardLiveWithLivenChannelEnum:(NSString *)livenChannelEnum
                                                livenessId:(NSString *)livenessId
                                               livenessImg:(NSString *)livenessImg
                              delegate:(id <RequestDelegate> _Nullable)delegate
                            success:(void (^)(NSDictionary * response, id mark))success
                            failure:(void (^)(NSString * errorStr, id mark))failure{
    
    NSDictionary * dic = @{
        KEY_livenessId: readStr(livenessId),          // livenessId - laical
        KEY_livenessImg: readStr(livenessImg),      // livenessImg - expiraria
    };
    NSString * url = [NSString stringWithFormat:@"%@%@/%@",URL_HEAD, AUTH_CHECK_MODIFY_NBANKCARD_LIVE,livenChannelEnum];
    [RequestApi postUrl:url delegate:delegate parameters:dic success:success failure:failure];
}


@end
